package maintain

import (
	"context"
	"gf_blog_admin/internal/dao"
	"gf_blog_admin/internal/model"
	"gf_blog_admin/internal/service"
)

type sLoginLog struct{}

func init() {
	service.RegisterLoginLog(NewLoginLog())
}

func NewLoginLog() *sLoginLog {
	return &sLoginLog{}
}

func (s *sLoginLog) List(ctx context.Context, in *model.LoginLogListInput) (out []*model.LoginLogListItem, total int, err error) {
	var (
		cls = dao.BlSysLoginLog.Columns()
		m   = dao.BlSysLoginLog.Ctx(ctx)
	)

	if in.Username != "" {
		m = m.Where(cls.Username, in.Username)
	}
	if in.StartDate != "" && in.EndDate != "" {
		m = m.WhereBetween(cls.CreatedAt, in.StartDate, in.EndDate)
	}

	out = make([]*model.LoginLogListItem, 0)
	err = m.OrderDesc(cls.Id).
		Fields(
			cls.UserId, cls.Username, cls.IpAddress, cls.DeviceInfo, cls.LogType,
			cls.Platform, cls.Browser, cls.LogRemark, cls.CreatedAt,
		).
		Page(in.Page, in.PageSize).
		ScanAndCount(&out, &total, false)
	return
}
